﻿using System.Collections.Generic;
using System.Data.Common;
using Jayden.Dll.Core;
using Jayden.Dll.Core.Persistence;
using Jayden.Dll.Core.Persistence.Data;
using Jayden.Dll.Core.Persistence.Data.SQLite;
using Jayden.Dll.Core.Persistence.Models;

namespace Jayden.Dll.Series.Providers
{
	public class SQLiteUserShowInfoDataAccess : SQLiteDataAccess<UserShowInfo>
	{
		public SQLiteUserShowInfoDataAccess(Configuration config, UnitOfWork uow, Entity entity) : base(config, uow, entity) { }


		public UserShowInfo Read(string username, string showId) { return Read(UnitOfWork.NewObjectId(Entity, username, showId)); }

		public UserShowInfo Create(string username, string showId)
		{
			UserShowInfo newUserRole = new UserShowInfo(UnitOfWork, username, showId);
			newUserRole.UserName = username;
			newUserRole.ShowInfoId = showId;
			Create(newUserRole);
			return newUserRole;
		}

		public void DeleteByUserName(string username)
		{
			string deleteCommandText = Helper.GenerateDelete(Entity.Table.TableName, "UserName=@UserName");
			using (DbCommandProxy deleteCommand = CreateCommand(deleteCommandText))
			{
				CreateParameter(deleteCommand, "UserName", username);
				int count = deleteCommand.Command.ExecuteNonQuery();
			}
		}

		public void DeleteByShowInfoId(string showId)
		{
			string deleteCommandText = Helper.GenerateDelete(Entity.Table.TableName, "ShowInfoId=@ShowInfoId");
			using (DbCommandProxy deleteCommand = CreateCommand(deleteCommandText))
			{
				CreateParameter(deleteCommand, "ShowInfoId", showId);
				int count = deleteCommand.Command.ExecuteNonQuery();
			}
		}

		public List<UserShowInfo> FindByUserName(string username)
		{
			string selectCommandText = Helper.GenerateSelect(null, Entity.Table.TableName, "UserName=@UserName", null, null, null, null);
			using (DbCommandProxy selectCommand = CreateCommand(selectCommandText))
			{
				CreateParameter(selectCommand, "UserName", username);
				using (DbDataReader reader = selectCommand.Command.ExecuteReader())
				{
					List<UserShowInfo> userRoles = new List<UserShowInfo>(EnumerateFromReader(reader));
					return userRoles;
				}
			}
		}

		public List<UserShowInfo> FindByShowInfoId(string showId)
		{
			string selectCommandText = Helper.GenerateSelect(null, Entity.Table.TableName, "ShowInfoId=@ShowInfoId", null, null, null, null);
			using (DbCommandProxy selectCommand = CreateCommand(selectCommandText))
			{
				CreateParameter(selectCommand, "ShowInfoId", showId);
				using (DbDataReader reader = selectCommand.Command.ExecuteReader())
				{
					List<UserShowInfo> userRoles = new List<UserShowInfo>(EnumerateFromReader(reader));
					return userRoles;
				}
			}
		}


		public void Delete(string username, string showId) { Delete(UnitOfWork.NewObjectId(Entity, username, showId)); }


		public List<UserShowInfo> FindByShowInfoId(string showId, string usernameLike)
		{
			string selectCommandText = Helper.GenerateSelect(null, Entity.Table.TableName, "ShowInfoId=@ShowInfoId AND UserName LIKE @UserName", null, null, null, null);
			using (DbCommandProxy selectCommand = CreateCommand(selectCommandText))
			{
				CreateParameter(selectCommand, "ShowInfoId", showId);
				CreateParameter(selectCommand, "UserName", usernameLike);
				using (DbDataReader reader = selectCommand.Command.ExecuteReader())
				{
					List<UserShowInfo> userRoles = new List<UserShowInfo>(EnumerateFromReader(reader));
					return userRoles;
				}
			}
		}
	}
}
